package com.kx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kx.entity.*;
import com.kx.mapper.GoodsCategoryMapper;
import com.kx.mapper.GoodsMapper;
import com.kx.service.GoodsService;
import com.kx.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public DataVO<GoodsVO> findData(Integer page, Integer limit) {
        DataVO<GoodsVO> dataVO = new DataVO<>();
        //1、设置code
        dataVO.setCode(0);
        //2、设置msg
        dataVO.setMsg("");
        //3、设置count(如果使用了mybatis-plus分页，则不需要调用其中selectCount方法单独调用)
//        dataVO.setCount(goodsMapper.selectCount(null));

//        List<Goods> goodsList= goodsMapper.selectList(null);
        //取消上方全查询，分页设置List<Goods>通过getRecords()获取
        IPage<Goods> goodsIPage = new Page<>(page, limit);
        IPage<Goods> result = goodsMapper.selectPage(goodsIPage, null);
        dataVO.setCount(result.getTotal());
        List<Goods> goodsList = result.getRecords();

        List<GoodsVO> goodsVOList = new ArrayList<>();
        for (Goods goods : goodsList) {
            GoodsVO goodsVO = new GoodsVO();
            BeanUtils.copyProperties(goods, goodsVO);

//            queryWrapper1.eq("id",goods.getCategoryleveloneId());
//            goodsVO.setCategorylevelone(goodsCategoryMapper.selectOne(queryWrapper1).getName());
            //如果上方代码种类1报了空指针异常，则数据库没有此数据，需要进行if(xx!=null)，才能正确获取到name，种类2、3也可仿1改
            QueryWrapper<GoodsCategory> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id", goods.getCategoryleveloneId());
            GoodsCategory goodsCategory1 = goodsCategoryMapper.selectOne(queryWrapper1);
            if (goodsCategory1 != null) {
                goodsVO.setCategorylevelone(goodsCategory1.getName());
            }
//            QueryWrapper queryWrapper2=new QueryWrapper();
//            queryWrapper2.eq("id",goods.getCategoryleveltwoId());
//            GoodsCategory goodsCategory2=goodsCategoryMapper.selectOne(queryWrapper2);
//            if (goodsCategory2!=null){
//                goodsVO.setCategorylevelone(goodsCategory2.getName());
//            }
//            QueryWrapper queryWrapper3=new QueryWrapper();
//            queryWrapper3.eq("id",goods.getCategorylevelthreeId());
//            GoodsCategory goodsCategory3= goodsCategoryMapper.selectOne(queryWrapper3);
//            if (goodsCategory3!=null){
//                goodsVO.setCategorylevelone(goodsCategory3.getName());
//            }

            goodsVOList.add(goodsVO);
        }
        //4、设置data
        dataVO.setData(goodsVOList);
        return dataVO;
    }

    @Override
    public GoodsVO findGoodVOById(Integer gid) {
        //先根据gid查到单条goods数据
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gid", gid);
        Goods goods = goodsMapper.selectOne(queryWrapper);
        //将查到的单条相同类型的goods数据拷贝到goodVO中
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goods, goodsVO);
        //根据goods中的查询单条商品类别的信息
        QueryWrapper<GoodsCategory> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id", goods.getCategoryleveloneId());
        if (goods.getCategoryleveloneId() != null) {
            GoodsCategory goodsCategory1 = goodsCategoryMapper.selectOne(queryWrapper1);
            goodsVO.setCategorylevelone(goodsCategory1.getName());
        }
        return goodsVO;
    }

    @Override
    public Goods findGoodById(Integer gid) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gid", gid);
        return goodsMapper.selectOne(queryWrapper);
    }

    @Override
    public int addGood(Goods goods) {
        return goodsMapper.insert(goods);
    }

    /**
     * 用deleteById(Serializable id)操作显示delete from goods where null=?
     *
     * @param gid
     * @return int
     */
    @Transactional
    @Override
    public int deleteGood(Integer gid) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gid", gid);
        return goodsMapper.delete(queryWrapper);
    }

    @Transactional
    @Override
    public int deleteGoodById(Integer gid) {
        return goodsMapper.deleteById(gid);
    }

    @Transactional
    @Override
    public int editGood(Goods goods) {
        return goodsMapper.updateById(goods);
    }

    @Transactional
    @Override
    public int editGood2(Goods goods) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gid", goods.getGid());
        return goodsMapper.update(goods, queryWrapper);
    }

    @Override
    public BarVO getBarVO() {
        List<GoodBarVO> list=goodsMapper.findAllGoodBarVO();
        List<String> names=new ArrayList<>();
        List<Integer> values=new ArrayList<>();
        for (GoodBarVO goodBarVO : list) {
            names.add(goodBarVO.getName());
            values.add(goodBarVO.getCount());
        }
        BarVO barVO=new BarVO();
        barVO.setNames(names);
        barVO.setValues(values);
        return barVO;
    }

//    @Override
//    public List<PieVO> getPieVO(){
//        List<GoodBarVO> list=goodsMapper.findAllGoodBarVO();
//        List<PieVO> pieVOList=list.stream().map(e -> new PieVO(e.getCount(), e.getName())).collect(Collectors.toList());
//        return pieVOList;
//    }
    @Override
    public List<GoodBarVO> getPieVO(){
        List<GoodBarVO> list=goodsMapper.findAllGoodBarVO();
        return list;
    }
}
